package com.nayun.gjh.xiaokabanghou.controller;

import com.nayun.gjh.xiaokabanghou.common.*;
import com.nayun.gjh.xiaokabanghou.model.request.FindCardRequest;
import com.nayun.gjh.xiaokabanghou.model.request.SafetyUserRequest;
import com.nayun.gjh.xiaokabanghou.model.response.SafetyDiuShiRecordResponse;
import com.nayun.gjh.xiaokabanghou.model.response.SafetyFinderUserResponse;
import com.nayun.gjh.xiaokabanghou.model.response.SafetyLoseUserResponse;
import com.nayun.gjh.xiaokabanghou.model.response.SafetyUserResponse;
import com.nayun.gjh.xiaokabanghou.service.DiuzhaoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@RestController
@RequestMapping("/card")
@CrossOrigin(origins = {"http://localhost:5173/"}, allowCredentials = "true")
public class DiuzhaoController {
    @Resource
    private DiuzhaoService diuzhaoService;

    @PostMapping("/search/loser")
    public BaseResponse<SafetyLoseUserResponse> cardSearch(@RequestBody FindCardRequest findCardRequest, HttpServletRequest httpServletRequest) throws Exception {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        String safetyCordCode = findCardRequest.getSafetyCordCode();
        if (safetyCordCode == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        SafetyLoseUserResponse safetyLoseUserResponse = diuzhaoService.searchLoseFindRecord(safetyCordCode,loginUser);
        return ResultUtils.success(safetyLoseUserResponse);
    }

    @PostMapping("/finder/upload")
    public BaseResponse<SafetyDiuShiRecordResponse> uploadLoseRecord(@RequestBody FindCardRequest findCardRequest, HttpServletRequest httpServletRequest) throws Exception {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        String safetyCordCode = findCardRequest.getSafetyCordCode();
        SafetyDiuShiRecordResponse safetyDiuShiRecordResponse = diuzhaoService.uploadLoseRecord(safetyCordCode, 10, loginUser);
        return ResultUtils.success(safetyDiuShiRecordResponse);
    }

    @GetMapping("/search/finder")
    public BaseResponse<SafetyFinderUserResponse> cardSearchFinder(HttpServletRequest httpServletRequest) throws Exception {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        SafetyFinderUserResponse safetyFinderUserResponse = diuzhaoService.searchFinder(loginUser);
        if (safetyFinderUserResponse == null) {
            throw new BusinessException(ErrorCode.DATA_NULL, "返回值为空");
        }
        return ResultUtils.success(safetyFinderUserResponse);
    }

    @GetMapping("/least")
    public BaseResponse<ArrayList> leastCardList() throws Exception {
        ArrayList<String> strings = diuzhaoService.leastCardList();
        return ResultUtils.success(strings);
    }
}
